import { EventMgr, EventName } from "../manager/EventMgr";
import LocalValues from "./LocalValues";
import USDate from "../comm/USDate";
import { GameConfig } from "./GameConfig";
import GameData from "../game/GameData";

/**
 * 玩家数据
 */
class UserModel {
    private _coin: number;
    private _curLevelId: number;
    private _winTimes: number; // 连胜次数
    private _birthTime: number;
    private _todayIdx: number;
    private _todayWinTimes: number;
    private _curLoseTimes: number;
    private _version: number;
    private _uid: number;
    public set uid(value: number) {
        if (this._uid != value) {
            this._uid = value;
            this.saveUserData();
        }
    }
    public get uid() {
        return this._uid;
    }
    public get curLoseTimes() {
        return this._curLoseTimes;
    }
    public addCurLoseTimes() {
        this._curLoseTimes++;
        this.saveUserData();
    }
    public resetCurLoseTimes() {
        this._curLoseTimes = 0;
        this.saveUserData();
    }
    public get todayWinTimes() {
        this.checkToday();
        return this._todayWinTimes;
    }
    public addTodayWinTimes() {
        this.checkToday();
        this._todayWinTimes++;
        this.saveUserData();
    }
    private checkToday() {
        if (this._todayIdx != USDate.dayIdx()) {
            this._todayWinTimes = 0;
            this._todayIdx = USDate.dayIdx();
            this.saveUserData();
        }
    }
    public loadUserData() {
        // 从local读取用户数据，目前用写死的数据
        // this.clearUserData();
        const userData = LocalValues.getUserData();
        if (!userData?.version || userData?.version < GameConfig.userDataVersion) {
            this.clearUserData();
            this._coin = 0;
            this._curLevelId = GameData.firstLevelId;
            this._winTimes = 0;
            this._birthTime = USDate.now();
            this._todayIdx = USDate.dayIdx();
            this._todayWinTimes = 0;
            this._curLoseTimes = 0;
            this._version = GameConfig.userDataVersion;
            this._uid = 0;
            this.saveUserData();
        } else {
            this._coin = userData.coin;
            this._curLevelId = userData.curLevelId;
            this._winTimes = userData.winTimes;
            this._birthTime = userData.birthTime;
            this._todayIdx = userData.todayIdx;
            this._todayWinTimes = userData.todayWinTimes;
            this._curLoseTimes = userData.curLoseTimes;
            this._version = userData.version;
            this._uid = userData.uid;
        }
    }
    private _saveTimeout: any;
    public saveUserData() {
        clearTimeout(this._saveTimeout);
        this._saveTimeout = setTimeout(()=>{
            // 保存用户数据
            const userData:IUserData = {
                coin: this._coin,
                curLevelId: this._curLevelId,
                winTimes: this._winTimes,
                birthTime: this._birthTime,
                todayIdx: this._todayIdx,
                todayWinTimes: this._todayWinTimes,
                curLoseTimes: this._curLoseTimes,
                version: this._version,
                uid: this._uid
            };
            LocalValues.setUserData(userData);
            console.log('saveUserData');
        }, 500);
    }
    public clearUserData() {
        console.log('clearUserData');
        LocalValues.clear();
    }
    public get curLevelId() { return this._curLevelId; }
    public nextLevel() {
        const oldLevelId = this._curLevelId;
        // if (this.curLevelId >= Cfgs.Level.getMaxLevelId()) {
        // }
        // const level = GameData.getNextLevel(this.curLevelId);
        // this._curLevelId = level.getLevelId();
        // EventMgr.emit(EventName.onCurLevelChange,this._curLevelId, oldLevelId);
        // this.saveUserData();
    }
    public setLevelId(levelId: number) {
        if (GameData.hasLevel(levelId)) {
            const oldLevelId = this._curLevelId;
            this._curLevelId = levelId;
            EventMgr.emit(EventName.onCurLevelChange,this._curLevelId, oldLevelId);
            this.saveUserData();
            return true;
        }
    }
    public get coin() {
        return this._coin;
    }
    public emitCoinChange(bDisplay = false) {
        EventMgr.emit(EventName.onGoldChange, this._coin);
        bDisplay && EventMgr.emit(EventName.refreshDisplayCoin, this._coin);
    }
    public addCoin(coin: number, bDisplay = true) {
        this._coin += coin;
        this.emitCoinChange(bDisplay);
        this.saveUserData();
    }
    public costCoin(coin: number, bDisplay = true) {
        if (this.isEnoughCoin(coin)) {
            this._coin -= coin;
            this.emitCoinChange(bDisplay);
            this.saveUserData();
            return true;
        }
        return false;
    }
    public setCoin(coin: number, bDisplay = true) {
        this._coin = coin;
        this.emitCoinChange(bDisplay);
        this.saveUserData();
    }
    public isEnoughCoin(coin: number) {
        return this._coin >= coin;
    }
    public addWinTimes() {
        if (true) {
            this._winTimes++;
            EventMgr.emit(EventName.onWinTimesChange, this._winTimes);
        }
        this._curLoseTimes = 0;
        this.addTodayWinTimes();
        this.saveUserData();
    }
    public breakWinTimes(bAdGiveup = false) {
        if (!bAdGiveup) {
            this._winTimes = 0;
            EventMgr.emit(EventName.onWinTimesChange, this._winTimes);
        }
        this._curLoseTimes++;
        this.saveUserData();
    }
    public setWinTimes(winTimes: number) {
        this._winTimes = winTimes;
        EventMgr.emit(EventName.onWinTimesChange, this._winTimes);
        this.saveUserData();
    }
    public get winCombo() {
        return this._winTimes;
    }
}

export default new UserModel();

export interface IUserData {
    coin: number;
    curLevelId: number;
    winTimes: number;
    birthTime: number;
    todayIdx: number;
    todayWinTimes: number;
    curLoseTimes: number;
    version: number;
    uid: number;
}